<?php

include "../../app/dao/blog.php";

class Blog_Dao{ 

	var $blog = array();
	var $blog_id;
	var $query;
	var $error;
    var $db;
    
    public function __construct(Database $db){
        $this->db = $db;
    }  
	
	//fungsi view blog
	function get_blog($id=null,$posisi=null, $batas=null){
	
		$this->db->connect->connect();
		
		if($this->db->table_Exists('blog')){
		
			if($id!=null AND $posisi==null AND $batas==null ){
			
				$this->query = "
								SELECT
								*
								FROM 
								blog 
								WHERE 
								blog_id='".$id."'
								";
			}else if($id!=null AND !is_null($posisi) AND !is_null($batas)){
                $this->query = "SELECT
								*
								FROM
								blog 
								WHERE 
								blog_id = '".$id."'
					
								LIMIT $posisi,$batas";
            }else if(!is_null($posisi) AND !is_null($batas)){
                $this->query = "SELECT
								*
								FROM
								blog                
																
								LIMIT $posisi,$batas";
			}else {
				$this->query = "SELECT
								*
								FROM 
								blog";
			}
			
			$result = mysql_query($this->query);
			
			if($result){
				
				while($r=mysql_fetch_array($result)){
				
					$blog = new Blog();
					
					$blog->blog_id = $r['blog_id'];
					$blog->date = $r['date'];
					$blog->title = $r['title'];
					$blog->content = $r['content'];
					$blog->image= $r['image'];
					$blog->contributor = $r['contributor'];
					$blog->rating = $r['rating'];
					$blog->publish = $r['publish'];
					
					$this->blog[] = $blog;
					
				}
				
				$this->db->connect->disconnect();
				return $this->blog;
				
			}else{
				
				$this->error = 'data tidak ditemukan';
				return $this->error;
				
			}
		}else{
			
			$this->error = 'data tidak ditemukan';
			return $this->error;
			
		}
	}
	
	function del($id){
	
		$this->db->connect->connect();
		
		if($this->db->table_Exists('blog')){
		
			if($id!=null){
			
				$this->query = "DELETE 
								FROM 
								blog 
								WHERE 
								blog_id='".$id."'
								";
			
			}else{
			
				$this->error = 'data tidak ditemukan';
				return $this->error;
				
			}
			
			mysql_query($this->query);
            $this->db->connect->disconnect();
		
		}else{
		
			$this->error = 'data tidak ditemukan';
			return $this->error;
			
		}
	}
	
	function publish($id){
	
		$this->db->connect->connect();
		
		if($this->db->table_Exists('blog')){
		
			if($id!=null){
			
				$this->query = "UPDATE 
								blog 
								SET 
								publish='Y' 
								WHERE 
								blog_id='".$id."'
								";
				//echo $this->query;
				
			}else{
			
				$this->error = 'data tidak ditemukan';
				return $this->error;
				
			}
			
			mysql_query($this->query);
            $this->db->connect->disconnect();
		
		}else{
		
			$this->error = 'data tidak ditemukan';
			return $this->error;
			
		}
	}
	
}
?>